/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Department;
import me.zhengjie.modules.system.domain.Departmentteacher;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.service.DepartmentService;
import me.zhengjie.modules.system.domain.vo.DepartmentQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.*;

import me.zhengjie.modules.system.service.DepartmentteacherService;
import me.zhengjie.modules.system.service.TeacherService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-12-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "部门管理生成管理")
@RequestMapping("/api/department")
public class DepartmentController {

    private final DepartmentService departmentService;

    private final DepartmentteacherService departmentteacherService;

    private final TeacherService teacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('department:list')")
    public void exportDepartment(HttpServletResponse response, DepartmentQueryCriteria criteria) throws IOException {
        departmentService.download(departmentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询部门管理生成")
    @ApiOperation("查询部门管理生成")
//    @PreAuthorize("@el.check('department:list')")
    public ResponseEntity<List<Map<String,Object>>> queryDepartment(DepartmentQueryCriteria criteria, Page<Object> page){

        List<Department> pageResult = departmentService.queryAll(criteria);
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<Integer> departmentIds = new ArrayList<>();
        for(Department department:pageResult){
            Map<String,Object> map = new HashMap<>();
            map.put("name",department.getName());
            map.put("schoolId",department.getSchoolId());
            map.put("id",department.getId());
            map.put("teacherId",null);
            map.put("userName",null);
            mapList.add(map);
            departmentIds.add(department.getId());
        }

        List<Departmentteacher> departmentteacherList = new ArrayList<>();
        if(departmentIds.size()!=0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("departmentId",departmentIds);
            queryWrapper.eq("type",1);
            departmentteacherList = departmentteacherService.list(queryWrapper);

            for(Map<String,Object> mapt1:mapList){
                for(Departmentteacher departmentteacher:departmentteacherList){
                    if(mapt1.get("id") == departmentteacher.getDepartmentId()){
                        mapt1.put("teacherId",departmentteacher.getTeacherId());
                        mapt1.put("departmentteacherId",departmentteacher.getId());
                    }
                }
            }
        }


        List<Integer> teacherIdList = new ArrayList<>();
        for(Departmentteacher departmentteacher:departmentteacherList){
            teacherIdList.add(departmentteacher.getTeacherId());
        }
        if(teacherIdList.size()!=0){
            //获取教师
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",teacherIdList);
            List<Teacher> teacherList = teacherService.list(queryWrapper1);

            for(Map<String,Object> mapt1:mapList){
                for(Teacher teacher:teacherList){

                    if(String.valueOf(mapt1.get("teacherId")).equals(String.valueOf(teacher.getId()))){

                        mapt1.put("userName",teacher.getNickName());

                    }
                }
            }
        }

//        PageResult<Map<String,Object>> pageResult1 = new PageResult<Map<String,Object>>();
        return new ResponseEntity<>(mapList,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增部门管理生成")
    @ApiOperation("新增部门管理生成")
//    @PreAuthorize("@el.check('department:add')")
    public ResponseEntity<Object> createDepartment(@Validated @RequestBody Department resources){

        departmentService.create(resources);
        return new ResponseEntity<>(resources.getId(),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改部门管理生成")
    @ApiOperation("修改部门管理生成")
//    @PreAuthorize("@el.check('department:edit')")
    public ResponseEntity<Object> updateDepartment(@Validated @RequestBody Department resources){
        departmentService.update(resources);
        return new ResponseEntity<>(resources.getId(),HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除部门管理生成")
    @ApiOperation("删除部门管理生成")
//    @PreAuthorize("@el.check('department:del')")
    public ResponseEntity<Object> deleteDepartment(@RequestBody List<Integer> ids) {
        departmentService.deleteAll(ids);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("departmentId",ids);
        departmentteacherService.remove(queryWrapper);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}